<?php
/**
 * Description of comments
 *
 * @author Flame
 */
class comments{

    private $comments,$what,$id_what;

    function __construct($what,$id_what)
    {
        global $db;

        $this->comments = array();
        $this->what = $what;
        $this->id_what = $id_what;

        $sql = "SELECT * FROM view_comments WHERE what = '".$this->what."' AND id_what = ".$this->id_what." WHERE delete = 0 ORDER BY tempo DESC";
        $result = $db->query($sql);
        while($row = mysql_fetch_assoc($result))
        {
            $p = new person($row['id_da'],$row['username'],$row['sesso'],$row['avatar'],$row['online'],$row['giorno'],$row['mese'],$row['anno']);
            $this->comments[] = new comment($row['id'],$p,$row['testo'],$row['tempo']);
        }
    }

    function send($mess)
    {
        global $db,$user;

        if($user->online)
        {
            $sql = "INSERT INTO comments (id_da,mess,tempo,what,id_what) VALUES ('".$user->id."','".funzione_per_testo($mess)."','".time()."','".$this->what."','".$this->id_what."')";
            $result = $db->query($sql);
        }
    return false;
    }


    function get(){ return $this->comments; }


    function erase()
    {
        global $db,$user;

        if($user->mod)
        {
            $sql = "DELETE FROM comments WHERE what = '".$this->what."' AND id_what = ".$this->id_what."";
            if($db->query($sql)) return true;
        }
        return false;
    }

    function total(){ return count($this->comments); }
}
?>
